package main

import (
	"fmt"
	"math/rand"
)

func main() {

	vg := &voteGame{students: []*student{
		&student{name: fmt.Sprintf("%d", 1)},
		&student{name: fmt.Sprintf("%d", 2)},
		&student{name: fmt.Sprintf("%d", 3)},
		&student{name: fmt.Sprintf("%d", 4)},
		&student{name: fmt.Sprintf("%d", 5)},
	},
	}
	leader := vg.goRun()
	fmt.Println(leader)
}

type student struct {
	name string
	agree int
	disagree int
}
type voteGame struct {
	students []*student
}
type leader = student // 返回的leader也是一个student
func (g *voteGame) goRun() *leader {
	for _, item := range g.students{
		randInt := rand.Int()
		if randInt % 2 ==0 {
			item.voteA(g.students[randInt%len(g.students)])
		} else {
			item.voteB(g.students[randInt%len(g.students)])
		}
	}
	maxScore := -1
	maxScoreIndex := -1
	for i, item := range g.students{
		if maxScore < item.agree{
			maxScore = item.agree
			maxScoreIndex =i
		}
	}
	if maxScoreIndex>=0 {
		return g.students[maxScoreIndex]
	}
	return nil
}

// 投给A
func (stu *student) voteA(target *student)  {
	target.agree++
}
// 投给B
func (stu *student) voteB(target *student)  {
	target.disagree++
}
